/**
 * Octopus, an XMMS2 client.
 * Copyright (C) 2014 Octopus Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace Octopus {
	/*
	 * Global singleton for access from View-Objects
	 */
	public class Global : Object {
		private XmmsClient _xmms_client = null;
		private Playback   _playback   = null;
		private ActivePlaylist  _active_playlist  = null;
		private Configure _configure;
		private Gee.List<string> _css_files;
		public string albumlist_load_query { get; private set; default = ""; }
		public string bindata_path { get; private set; }
		public string data_path { get; private set; }

		public signal void init_finished ();
		public signal void message (string msg);

		private Global () {
			_xmms_client = new XmmsClient ();
			_playback   = new Playback ();
			_active_playlist  = new ActivePlaylist ();
			_configure = new Configure ();
			_css_files = new Gee.ArrayList<string> ();

			_xmms_client.init ();

			GLib.debug ("PACKAGE:  %s", Config.PACKAGE);
			GLib.debug ("DATAPATH: %s", Config.DATAPATH);
			GLib.debug ("LIBPATH:  %s", Config.LIBPATH);
			data_path = Config.DATAPATH;
		}

		private static Global _instance;

		public static new Global get () {
			if (_instance == null) {
				_instance = new Global ();
			}
			return _instance;
		}

		public void css_files_add (string css_file) {
			if (css_file != "") {
				GLib.debug ("CSS_FILE:  %s", css_file);
				this._css_files.add (css_file);
			}
		}

		public int css_files_size () {
			return this._css_files.size;
		}

		public string css_files_get (int index) {
			return this._css_files.get (index);
		}

		public void set_query (string query) {
			if (query != "") {
				GLib.debug ("QUERY:  %s", query);
				this.albumlist_load_query = query;
			}
		}

		public XmmsClient get_xmms_client () {
			return _xmms_client;
		}

		public ActivePlaylist get_active_playlist () {
			return _active_playlist;
		}

		public Playback get_playback () {
			return _playback;
		}

		public Configure get_configure () {
			return _configure;
		}

		public void init () {
			//async version
			var result = _xmms_client.get_xmms ().config_get_value ("bindata.path");
			result.notifier_set( (/*Xmms.Value*/ val) => {
				unowned string config_string;
				if (val.get_string(out config_string)) {
					bindata_path = config_string;
				}
				init_finished ();
				return true;
			});
			result.wait ();
		}

	}
}
